package game.controller
{
	import game.common.Reflection;
	import game.event.EventName;
	import game.event.GameEvent;
	import game.managers.LayerManager;
	import game.managers.UIManager;
	import game.model.SceneModel;
	import game.model.vo.SceneInfoVo;
	import game.view.scene.Scene;

	public class SceneCenter extends BaseCenter
	{
		private static var _instance:SceneCenter;
		private var _model:SceneModel=SceneModel.getInstance();
		private var _scene:Scene;

		public function SceneCenter()
		{
			if(_instance == null){
				_instance = this;
				init();
				addEvent();
			}else
				throw new Error("SeneCenter is single instance");
		}
		
		public static function getInstance():SceneCenter
		{
			if(_instance == null)
				_instance = new SceneCenter();
			return _instance;
		}
		
		private function addEvent():void
		{
			/**侦听开始煮菜事件*/
			dispatcher.addEventListener(EventName.COOKER_COOK,onCooking);
			/**侦听顾客开始吃东西*/
			dispatcher.addEventListener(EventName.SERV_COMPLETE,changeCustomerEat);
			/**侦听顾客吃完东西*/
			dispatcher.addEventListener(EventName.CUSTOMER_EATFINISH,customerEatFinish);
			/**侦听结账结束*/
			dispatcher.addEventListener(EventName.PAY_FINISH,onPayFinish);
			/**侦听添加顾客*/
			dispatcher.addEventListener(EventName.ADD_CUSTOMER,addCustomer);
			/**无食物*/
			dispatcher.addEventListener(EventName.NO_FOOD,noFood);
		}

		
		/**
		 * 初始化场景
		 * */
		private function init():void
		{
			_scene=new Scene();
			UIManager.scene = _scene;
			LayerManager.sceneLayer.addChildAt(_scene,0);
			var sceneInfo:SceneInfoVo = new SceneInfoVo();
			
			sceneInfo.xml = Reflection.xmlLoader.content as XML;
			
			sceneInfo.gridSize = 40;
			sceneInfo.width= 8;
			sceneInfo.length= 11;
			sceneInfo.height=0;
			
			_model.buildScene(sceneInfo);
		}
			
		
		private function noFood(e:GameEvent):void
		{
			_scene.onNoFoodLeaving();
		}
		
		private function addCustomer(e:GameEvent):void
		{
			if (_scene.isWaiting == true) 
			{
				return;
			}
			_scene.addRole();
		}
		
		
		private function onCooking(e:GameEvent):void
		{
			_scene.onCookingStart();
		}
		
		
		private function changeCustomerEat(e:GameEvent):void
		{
			_scene.onCustomerEating(); 
		}
		
		
		private function customerEatFinish(e:GameEvent):void
		{
			_scene.onCustomerEatFinish();
		}
		
		
		private function onPayFinish(e:GameEvent):void
		{
			_scene.onPayingFinish();
		}
	}
}